package com.hpf.tool.utils;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;

import com.hpf.tool.exceptions.FileException;

import sun.misc.BASE64Decoder;
/**
 * 
 * @author hpf
 *
 */
public final class UtilsFile {
	/**
	 * 小数点
	 */
	private static String POINT = ".";
	
	/**
	 * 创建文件
	 * 
	 * @param file
	 *            创建的文件
	 * @return 创建后的文件
	 */
	public static File createFile(String file) {
		return createFile(new File(file));
	}

	/**
	 * 创建文件
	 * 
	 * @param file
	 *            创建的文件
	 * @return 创建后的文件
	 */
	public static File createFile(File file) {
		if (file == null) {
			throw new FileException(true, "file is null");
		}
		final String path = file.getPath();
		if (file.exists()) {
			if (file.isFile() == false) {
				throw new FileException(true, MessageFormat.format("{0} is not file", path));
			}
			return file;
		}
		/* 文件不存在，继续走创建流程 */
		if (path.indexOf(POINT) == -1) {
			throw new FileException(true, MessageFormat.format("{0} is not file", path));
		}

		try {
			file.getParentFile().mkdirs();
			file.createNewFile();
			return file;
		} catch (IOException e) {
			throw new FileException(true, MessageFormat.format("file create failure.{0}", path), e);
		}
	}

	public static void main(String[] args) {
		UtilsFile.createFolder("D:/test/a/b");
	}

	/**
	 * 创建文件夹(不存在则创建)
	 * 
	 * @param file
	 * @return
	 */
	public static File createFolder(String path) {
		return createFolder(new File(path));
	}

	/**
	 * 创建文件夹(不存在则创建)
	 * 
	 * @param file
	 * @return
	 */
	public static File createFolder(File file) {
		if (file == null) {
			throw new FileException(true, "file is null");
		}
		// 不存在则创建
		if (file.exists() == false) {
			file.mkdirs();
		}
		// 验证是否为文件路径
		if (file.isFile()) {
			return file.getParentFile();
		} else {
			// 验证项是否为目录
			if (file.isDirectory() == false) {
				throw new FileException(true, MessageFormat.format("目标路径有误：{0}", file.getPath()));
			}
			return file;
		}
	}

	/**
	 * 删除指定的文件
	 * 
	 * @param filePath
	 *            要删除的文件
	 */
	public static void deleteFile(String filePath) {
		deleteFile(new File(filePath));
	}

	/**
	 * 删除指定的文件
	 * 
	 * @param file
	 *            要删除的文件
	 */
	public static void deleteFile(File file) {
		if (file == null) {
			throw new FileException(true, "file is null");
		}
		final String path = file.getPath();
		if (!file.exists()) {
			throw new FileException(true, MessageFormat.format("({0}) is not exist", path));
		}
		if (!file.isFile()) {
			throw new FileException(true, MessageFormat.format("({0}) is not file", path));
		}
		if (file.delete() == false) {
			throw new FileException(true, MessageFormat.format("delete ({0}) Failure", path));
		}
	}

	/**
	 * 拷贝文件
	 * 
	 * @param source
	 * @param target
	 */
	public static void fileCopy(InputStream source, String target) {
		fileCopy(source, new File(target));
	}
	/**
	 * 拷贝文件
	 * 
	 * @param source
	 * @param target
	 */
	public static void fileCopy(InputStream source, File target) {
		createFile(target);
		try {
			fileCopy(source, new FileOutputStream(target));
		} catch (FileNotFoundException e) {
			throw new FileException(true, "创建文件输出流异常", e);
		}
	}

	/**
	 * 拷贝文件
	 * 
	 * @param source
	 * @param target
	 */
	public static void fileCopy(InputStream source, OutputStream target) {
		if (source == null) {
			throw new FileException(true, "输入流(InputStream) is null");
		}
		if (target == null) {
			throw new FileException(true, "输出流(OutputStream) is null");
		}
		try {

			int byteread = 0;
			byte[] buf = new byte[1024];
			while ((byteread = source.read(buf)) != -1) {
				target.write(buf, 0, byteread);
			}
		} catch (IOException e) {
			throw new FileException(true, "文件流读写异常", e);
		} finally {
			colse(source);
			colse(target);
		}
	}
	/**
	 * 拷贝文件
	 * @param source BASE64Decoder 编码过的 byte[]
	 * @param target
	 */
	public static void fileCopy(String source, String target) {
		fileCopy(source, new File(target));
	}
	/**
	 * 拷贝文件
	 * @param source BASE64Decoder 编码过的 byte[]
	 * @param target
	 */
	public static void fileCopy(String source, File target) {
		createFile(target);
		try {
			fileCopy(source, new FileOutputStream(target));
		} catch (FileNotFoundException e) {
			throw new FileException(true, "创建文件输出流异常", e);
		}

	}
	/**
	 * 拷贝文件
	 * @param source BASE64Decoder 编码过的 byte[]
	 * @param target
	 */
	public static void fileCopy(String source, OutputStream target) {
		if (UtilsString.isEmpty(source)) {
			throw new FileException(true, "BASE64Decoder(source) is null");
		}
		if (target == null) {
			throw new FileException(true, "输出流(OutputStream) is null");
		}
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] bytes = decoder.decodeBuffer(source);
			for (int i = 0; i < bytes.length; ++i) {
				// 调整异常数据
				if (bytes[i] < 0) {
					bytes[i] += 256;
				}
			}
			target.write(bytes);
			target.flush();
		} catch (IOException e) {
			throw new FileException(true, "文件流读写异常", e);
		} finally {
			colse(target);
		}
	}

	/**
	 * 关闭流
	 * 
	 * @param 流
	 * @throws IOException
	 */
	private static void colse(Closeable resource) {
		if (resource == null) {
			return;
		}
		try {
			resource.close();
		} catch (IOException e) {
			throw new FileException(false, "close InputStream Failure", e);
		}
	}
}
